package com.mall.order.web;

import com.mall.order.exception.NoStackException;
import com.mall.order.service.OrderService;
import com.mall.order.vo.OrderConfirmVo;
import com.mall.order.vo.OrderResponseVo;
import com.mall.order.vo.OrderSubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class OrderWebController {
    @Autowired
    private OrderService orderService;
    @GetMapping("/toTrade")
    public String toTrade(Model model){
        OrderConfirmVo confirmVo = orderService.confirmOder();
        model.addAttribute("confirmVo",confirmVo);
        return "conffirm";
    }

    @PostMapping("/orderSubmit")
    public String orderSubmit(OrderSubmitVo vo,Model model){
        Integer code = 0;
        OrderResponseVo orderResponseVo = null;
        try {
             orderResponseVo = orderService.orderSubmit(vo);
            code = orderResponseVo.getCode();
        } catch (NoStackException e) {
            code = 2;
        }
            if (code == 0){
                model.addAttribute("orderResponse");
                return "pay";
            }
            else {
                String msg = "订单失败";
                if (code == 1){
                    msg = msg+ "重复提交";
                }
                else {
                    msg += msg + "锁定库存失败";
                }
                System.out.println("code = " + code);
                return "redirect:http://order.msb.com/toTrade";
            }

    }
}
